<?php

/**
 * @author chenshubo
 * 威廉国际订单
 */

namespace App\Service\OrderService\OrderRouteService;
use App\Service\Code\Normal;
use Service\AppService\IdService;
use App\Model\PssModel\OrderSupplier;
use App\Service\HttpServer\HttpCurl;
use App\Service\OrderService\BaseSupplierRouterService;
use App\Model\PssModel\OrderSupplierGoods;
use App\Service\VendorClient\ShopWlgjApi\ShopWlgjApiClient;

class Shopwlgj extends BaseSupplierRouterService
{
    /**
     * 创建订单
     * @param array $param
     * @return mixed
     */
    public function createOrder( $param = [] )
    {
        //生成供应商主单号
        $supplierCode = $param['supplierGoodsInfo']['supplierCode'];
        //下游是否申报
        $isDelare = (
            (isset($param['order']['isOrderDeclare']) && $param['order']['isOrderDeclare'] == 1) ||
            (isset($param['order']['isPayDeclare']) && $param['order']['isPayDeclare'] == 1)
            ) ? true : false;
        //是否云仓商城
        $isShop = isset($param['order']['isSplitOrder']) && $param['order']['isSplitOrder'] == 1 ? true : false ;        
        
        $param['order']['supplier_psn'] = OrderSupplier::instance()->getCreateOrderSn($supplierCode);
        $param['order']['supplier_csn'] = IdService::instance()->getOtherID();
        //防止订单号重复问题,下游已申报，则使用下游的订单号作为中台子单号
        if( $isDelare && !$isShop ) {
            $param['order']['supplier_dsn'] = $param['order']['orderSn'];
        } else {
            $param['order']['supplier_dsn'] = IdService::instance()->getOtherID();
        }
        
        $res = OrderSupplier::instance()->getSupplierCreateOrderData($param);
        if( $res['code'] != Normal::SUC ) {
            return $res;
        }
        
        $orderItems[] = $res['data'];
        return Normal::returnCode(Normal::SUC,"获取成功",[
            'supplierCode' => $supplierCode,
            'orderItems' => $orderItems
        ]);
    }
    
    /**
     * 创建订单（刷单）
     * @param array $param
     * @return mixed
     */
    public function orderPay( $param = [] )
    {
        return $this->callSdkApi(ShopWlgjApiClient::class,$param['apiBody'])->createOrder($param);
    }
    
    /**
     * 订单取消
     * @param array $param
     * @return mixed
     */
    public function orderCancel( $param = [] )
    {
        return Normal::returnCode(Normal::SUC,"处理成功");
    }
    
    /**
     * 订单售后
     * @param array $param
     * @return mixed
     */
    public function orderAfterSale($param = [])
    {
        return $this->callSdkApi(ShopWlgjApiClient::class,$param['apiBody'])->cancelOrder($param);
    }
    
    /**
     * 获取物流信息
     * @param array $param
     */
    public function orderExpress( $param = [] )
    {
        if( empty($param['supplier_dsn']) || empty($param['supplier_csn']) ) {
            return Normal::returnCode(Normal::FAIL,"except param supplier_dsn or supplier_csn");
        }
        
        $supplier_dsn = $param['supplier_dsn'];
        $orderInfo = OrderSupplier::instance()->info([
            'supplier_dsn'   => $supplier_dsn,
            'supplier_csn'   => $param['supplier_csn'],
            '_fields_' => [
                'id',
                'supplier_dsn',
                'supplier_csn',
            ]
        ]);
        
        if( empty($orderInfo) ) {
            return Normal::returnCode(Normal::FAIL,"订单信息不能为空");
        }
        
        $orderGoods = OrderSupplierGoods::instance()->lists([
            'order_id'   => $orderInfo['id'],
            '_fields_' => [
                'supplier_skucode',
            ]
        ]);
        
        if( empty($orderGoods) ) {
            return Normal::returnCode(Normal::FAIL,"订单商品信息不能为空");
        }
        
        $expressInfo = [];
        foreach ($orderGoods as $item ) 
        {
            $res = $this->callSdkApi(ShopWlgjApiClient::class,$param['apiBody'])->orderExpress([
                'orderNo'   => $supplier_dsn,
                'proSkuNo'  => $item['supplier_skucode']
            ]);
            
            if( $res['code'] == Normal::SUC && !empty($res['data']['Result']) ) 
            {
                foreach ($res['data']['Result'] as $va ) {
                    $expressInfo[] = [
                        'express_name'  => $va['ExpName'],
                        'express_sn'    => $va['ExpNum'],
                    ];
                }
            }
        }
        
        if( empty($expressInfo) ) {
            return Normal::returnCode(Normal::FAIL,"订单未发货");
        }
        
        $express_name = implode(",", array_column($expressInfo, 'express_name'));
        $express_sn   = implode(",", array_column($expressInfo, 'express_sn'));
        return  Normal::returnCode(Normal::SUC,"已发货",[
            'expressInfo' => [
                'express_name'  => $express_name,
                'express_sn'    => $express_sn,
            ]
        ]);
    }
    
    /**
     * 获取响应code
     * @param array $param
     */
    public function getCode( $param = [])
    {
        $res = HttpCurl::instance()->getMicorServiceResponseInfo($param);
        if( $res['code'] == Normal::SUC ) {
            return Normal::returnCode($res['code'],"OK",[
                'supplier_csn' => $res['data']['orderCode']
            ]);
        }
        return $res;
    }
}